import VueRouter from "vue-router";
import Index from "@/views/Index.vue";
import Login from "@/views/Login.vue";
import Welcome from "@/components/Welcome.vue";
import GoodsAdmin from "@/components/GoodsAdmin.vue";
import GoodsStatistics from "@/components/GoodsStatistics.vue";
import AdminSetting from "@/components/AdminSetting.vue";
import GoodsLogin from "@/components/GoodsLogin.vue";
import UserSetting from "@/components/UserSetting.vue";
import GoodsOut from "@/components/GoodsOut.vue";
import outMoney from "@/components/OutMoney.vue";
import BuyCheck from "@/components/BuyCheck.vue";
import GoodsInfoLogin from "@/components/GoodsInfoLogin.vue";

const router = new VueRouter({
    routes: [
        {
            //登录组件
            path: '/',
            component: Login
        },
        {
            //主界面组件
            path: '/index',
            component: Index,
            //声明子路由
            children: [
                {
                    path: 'goodsInfoLogin',
                    component: GoodsInfoLogin
                },
                {
                    path: 'buyCheck',
                    component: BuyCheck
                },
                {
                    path: 'outMoney',
                    component: outMoney
                },
                {
                    path: 'goodsOut',
                    component: GoodsOut
                },
                {
                    //欢迎界面组件
                    path: '',
                    component: Welcome
                },
                {
                    // 商品管理组件
                    path: 'goodsAdmin',
                    component: GoodsAdmin
                },
                {
                    // 订单统计组件
                    path: 'goodsStatistics',
                    component: GoodsStatistics
                },
                {
                    // 管理员用户管理组件
                    path: 'adminSetting',
                    component: AdminSetting
                },
                {
                    // 商品入库组件
                    path: 'goodsLogin',
                    component: GoodsLogin
                },
                {
                    // 订单管理组件
                    path: 'userSetting',
                    component: UserSetting
                },
            ]
        }

    ]
})

//路由的前置守卫
router.beforeEach((to, from, next) => {
    //  console.info("前置守卫被调用");
    //获取sessionStorage中session_user这个名称
    let user = sessionStorage.getItem("session_user");
    // console.info("user对象在路由守卫中的值", user);
    //判断user如果不存在 并且访问的路径不是 /
    // console.info("to", to);
    if (!user && to.path !== "/") {
        //跳转到登录界面
        next("/");
    } else {
        //继续往下执行
        next()
    }


})

export default router
